package com.yao.spider.proxy.entity;

import java.io.Serializable;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created by 单耀 on 2017/12/5.
 */
/**
 * 接口说明
 * Delayed 用来标记那些应该在给定延迟时间之后执行的对象。
 */
public class Proxy implements Delayed,Serializable {
    private static final long serialVersionUID = -7583883432417635332L;
    private long timeIntervsl;//任务间隔时间
    private String ip;
    private int port;
    private boolean availableFlag;
    private boolean anonymousFlag;
    private long lastSuccessfulTime;//最近一次请求成功时间
    private long successfulTotalTime;//请求总耗时
    private int failureTimes;//请求失败次数
    private int successfulTimes;//请求成功次数
    private double successfulAverageTime;//成功请求平均耗时

    public Proxy(String ip, int port, long delayTime) {
        this.ip = ip;
        this.port = port;
        this.timeIntervsl = delayTime;
        this.timeIntervsl = TimeUnit.NANOSECONDS.convert(delayTime,TimeUnit.MILLISECONDS) + System.nanoTime();
    }

    /**
     * 返回与此对象相关的剩余延迟时间，以给定的时间单位表示。
     * @param unit
     * @return
     */
    public long getDelay(TimeUnit unit) {
        return unit.convert( timeIntervsl - System.nanoTime(),TimeUnit.SECONDS);
    }

    public int compareTo(Delayed o) {
        return 0;
    }

    public long getTimeIntervsl() {
        return timeIntervsl;
    }

    public void setTimeIntervsl(long timeIntervsl) {
        this.timeIntervsl = timeIntervsl;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public boolean isAvailableFlag() {
        return availableFlag;
    }

    public void setAvailableFlag(boolean availableFlag) {
        this.availableFlag = availableFlag;
    }

    public boolean isAnonymousFlag() {
        return anonymousFlag;
    }

    public void setAnonymousFlag(boolean anonymousFlag) {
        this.anonymousFlag = anonymousFlag;
    }

    public long getLastSuccessfulTime() {
        return lastSuccessfulTime;
    }

    public void setLastSuccessfulTime(long lastSuccessfulTime) {
        this.lastSuccessfulTime = lastSuccessfulTime;
    }

    public long getSuccessfulTotalTime() {
        return successfulTotalTime;
    }

    public void setSuccessfulTotalTime(long successfulTotalTime) {
        this.successfulTotalTime = successfulTotalTime;
    }

    public int getFailureTimes() {
        return failureTimes;
    }

    public void setFailureTimes(int failureTimes) {
        this.failureTimes = failureTimes;
    }

    public int getSuccessfulTimes() {
        return successfulTimes;
    }

    public void setSuccessfulTimes(int successfulTimes) {
        this.successfulTimes = successfulTimes;
    }

    public double getSuccessfulAverageTime() {
        return successfulAverageTime;
    }

    public void setSuccessfulAverageTime(double successfulAverageTime) {
        this.successfulAverageTime = successfulAverageTime;
    }


    public String getProxyStr() {
        return ip + ":" + port;
    }

    @Override
    public String toString() {
        return "Proxy{" +
                "timeIntervsl=" + timeIntervsl +
                ", ip='" + ip + '\'' +
                ", port=" + port +
                ", availableFlag=" + availableFlag +
                ", anonymousFlag=" + anonymousFlag +
                ", lastSuccessfulTime=" + lastSuccessfulTime +
                ", successfulTotalTime=" + successfulTotalTime +
                ", failureTimes=" + failureTimes +
                ", successfulTimes=" + successfulTimes +
                ", successfulAverageTime=" + successfulAverageTime +
                '}';
    }
}
